package com.wetao.module_user.fragment;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import androidx.appcompat.widget.AppCompatButton;
import androidx.appcompat.widget.AppCompatTextView;

import com.wetao.lib_common.EventBus.EventBusMessage;
import com.wetao.lib_common.base.BaseLazyFragment;
import com.wetao.lib_common.utils.BaseEvent;
import com.wetao.lib_common.view.OnMultiClickListener;
import com.wetao.module_user.R;
import com.wetao.module_user.R2;
import com.wetao.module_user.contract.UserClassAdminFragmentCon;
import com.wetao.module_user.presenter.UserClassAdminFragmentPre;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.OnClick;

public class UserClassAdminFragment extends BaseLazyFragment<UserClassAdminFragmentCon.View, UserClassAdminFragmentPre> implements
        UserClassAdminFragmentCon.View {


    AppCompatTextView tvClassAdmin;
    AppCompatButton btUpdate;

    public static UserClassAdminFragment newInstance(String className, Integer classId) {
        Bundle args = new Bundle();
        args.putString("className", className);
        args.putInt("classId", null == classId ? -1 : classId);

        UserClassAdminFragment fragment = new UserClassAdminFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public int getLayoutId() {
        return R.layout.user_fragment_admin_class;
    }

    @Override
    public UserClassAdminFragmentPre createPresenter() {
        return new UserClassAdminFragmentPre(mContext);
    }

    @Override
    public UserClassAdminFragmentCon.View createView() {
        return this;
    }

    @Override
    public void init(View rootView) {
        tvClassAdmin = rootView.findViewById(R.id.tv_class_admin);
        btUpdate = rootView.findViewById(R.id.bt_update);
        btUpdate.setOnClickListener(new OnMultiClickListener() {
            @Override
            public void onMultiClick(View v) {
                if(null != onListenClick) {
                    onListenClick.onListenClick();
                }
            }
        });

        Bundle bundle = null;
        if ((bundle = getArguments()) != null) {
            String className = bundle.getString("className");
            if(!TextUtils.isEmpty(className)) {
                tvClassAdmin.setText(className);
            }
            int classId = bundle.getInt("classId");
        }

    }

    @Override
    public void onFragmentFirst() {
        super.onFragmentFirst();
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
    }

    @Override
    public void onFragmentInVisible() {
        super.onFragmentInVisible();
    }

    @Override
    public void onFragmentVisble() {
        super.onFragmentVisble();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void busGetMessage(EventBusMessage messageEvent) {
        if(messageEvent.getAction() == EventBusMessage.EVENT_BUS_ACTION_UPDATE_CLASS_ADMIN) {
            tvClassAdmin.setText(messageEvent.getMsg());
        }
    }



    public interface OnListenClick {
        void onListenClick();
    }
    OnListenClick onListenClick;

    public void setOnListenClick(OnListenClick onListenClick) {
        this.onListenClick = onListenClick;
    }
}
